package com.pd.Singleton;

/**
 * Created with IntelliJ IDEA.
 * User: 5
 * Date: 16-12-14
 * Time: 下午4:23
 * To change this template use File | Settings | File Templates.
 * 懒汉式 双锁
 */
public class DoubleCheckSingleton {

    private static DoubleCheckSingleton singleton;


    public static DoubleCheckSingleton getSingleton() {
        if (singleton == null) {     //第一次检查
            synchronized (DoubleCheckSingleton.class) {  //不同步到话多线程不安全
                if (singleton == null) {        //第二次检查
                    singleton = new DoubleCheckSingleton();
                }
            }
        }
        return singleton;
    }

    public static void main(String args[]) {
        for (int i = 0; i < 15; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(DoubleCheckSingleton.getSingleton());
                }
            }).start();
        }
    }


}
